#include "register.h"
#include "ui_register.h"
#include "myprotocol.h"

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    this->setFixedSize(this->size().width(),this->size().height());
    this->setWindowIcon(QIcon(":/new/prefix1/Image/mesage.png"));
    this->setWindowTitle("REGISTER/DELETE");
    QPalette qp = this->palette(); //重新拷贝一份和本对象palette相同的palette
    qp.setBrush(QPalette::Window,QBrush(QImage(":/new/prefix1/Image/Frame.jpg").scaled(this->size())));
    this->setPalette(qp);

    this->socket = nullptr;
}

Register::~Register()
{
    delete ui;
}

void Register::set_socket(QTcpSocket* socket)
{
    this->socket = socket;
}

void Register::on_log_btn_clicked()
{
    char send_info[512] = {0};
    QByteArray usrname = this->ui->account_line->text().toUtf8();
    QByteArray password = this->ui->password_line->text().toUtf8();
    QByteArray name = this->ui->name_line->text().toUtf8();

    trans_usrinfo_t myinfo;
    sprintf(send_info,"%d",register_request_protocol);
    strcpy(myinfo.name,name.data());
    strcpy(myinfo.usrname,usrname.data());
    strcpy(myinfo.password,password.data());
    memcpy(send_info + 2,&myinfo,sizeof(myinfo));
    this->socket->write(send_info,sizeof(send_info));
}

void Register::on_delete_btn_clicked()
{
    char send_info[512] = {0};
    QByteArray name = this->ui->hrname_line->text().toUtf8();
    trans_usrinfo_t myinfo;
    sprintf(send_info,"%d",delete_usr_request_protocl);
    strcpy(myinfo.name,name.data());
    memcpy(send_info + 2,&myinfo,sizeof(myinfo));
    this->socket->write(send_info,sizeof(send_info));
}

void Register::on_back_btn_clicked()
{
    emit this->my_back_to_managerop_signal();
    this->hide();
    this->ui->name_line->clear();
    this->ui->account_line->clear();
    this->ui->password_line->clear();
    this->ui->hrname_line->clear();
}
